package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.PurchaseLine;
import com.zyd.shiro.business.service.PakPurchaseLineService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakPurchaseLine;
import com.zyd.shiro.persistence.mapper.PakPurchaseLineMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakPurchaseLineServiceImpl implements PakPurchaseLineService {

    @Autowired
    private PakPurchaseLineMapper pakPurchaseLineMapper;

    @Override
    public PurchaseLine insert(PurchaseLine purchaseLine) {
        Assert.notNull(purchaseLine, "对象不可为空！");
        purchaseLine.getPakPurchaseLine().setUpdateTime(new Date());
        purchaseLine.getPakPurchaseLine().setCreateTime(new Date());
        pakPurchaseLineMapper.insertSelective(purchaseLine.getPakPurchaseLine());
        return purchaseLine;
    }

    @Override
    public void insertList(List<PurchaseLine> purchaseLines) {
            for(PurchaseLine purchaseLine:purchaseLines){
               purchaseLine.getPakPurchaseLine().setUpdateTime(new Date());
               purchaseLine.getPakPurchaseLine().setCreateTime(new Date());
               pakPurchaseLineMapper.insertSelective(purchaseLine.getPakPurchaseLine());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakPurchaseLineMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(PurchaseLine purchaseLine) {
        Assert.notNull(purchaseLine, "对象不可为空！");
        purchaseLine.setUpdateTime(new Date());
        return pakPurchaseLineMapper.updateByPrimaryKey(purchaseLine.getPakPurchaseLine()) > 0;
    }

    @Override
    public boolean updateSelective(PurchaseLine purchaseLine) {
        Assert.notNull(purchaseLine, "对象不可为空！");
        purchaseLine.setUpdateTime(new Date());
        return pakPurchaseLineMapper.updateByPrimaryKeySelective(purchaseLine.getPakPurchaseLine()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public PurchaseLine getByPrimaryKey(Long primaryKey) {
        PakPurchaseLine pakPurchaseLine = pakPurchaseLineMapper.selectByPrimaryKey(primaryKey);
        return null == pakPurchaseLine ? null : new PurchaseLine(pakPurchaseLine);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public PurchaseLine getOneByEntity(PurchaseLine test) {
        Assert.notNull(test, "对象不可为空！");
        PakPurchaseLine pakPurchaseLine = pakPurchaseLineMapper.selectOne(test.getPakPurchaseLine());
        return null == pakPurchaseLine ? null : new PurchaseLine(pakPurchaseLine);
    }

    @Override
    public List<PurchaseLine> listAll() {
        List<PakPurchaseLine> pakPurchaseLines = pakPurchaseLineMapper.selectAll();
        if (CollectionUtils.isEmpty(pakPurchaseLines)) {
            return null;
        }
        List<PurchaseLine> purchaseLines = new ArrayList<>();
        for (PakPurchaseLine pakPurchaseLine : pakPurchaseLines) {
            purchaseLines.add(new PurchaseLine(pakPurchaseLine));
        }
        return purchaseLines;
    }

    @Override
    public List<PurchaseLine> listByEntity(PurchaseLine entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<PurchaseLine> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakPurchaseLine> pakPurchaseLines = pakPurchaseLineMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakPurchaseLines)) {
            return null;
        }
        List<PurchaseLine> purchaseLines = new ArrayList<>();
        for (PakPurchaseLine pakPurchaseLine : pakPurchaseLines) {
            purchaseLines.add(new PurchaseLine(pakPurchaseLine));
        }
        PageInfo bean = new PageInfo<PakPurchaseLine>(pakPurchaseLines);
        bean.setList(purchaseLines);
        return bean;
    }
@Override
    public void increase(PakPurchaseLine pakPurchaseLine) {
        pakPurchaseLineMapper.insert(pakPurchaseLine);
    }

    @Override
    public List<PakPurchaseLine> findByPurchase(Long purchaseId) {
        return pakPurchaseLineMapper.findByPurchase(purchaseId);
    }

    @Override
    public void increase(PakPurchaseLine purchaseLine, Long purchaseId) {

    }


}
